package com.share.sharelibrary;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QiQi on 15/12/3.
 */
public class MultiTypeAdapter extends RecyclerView.Adapter<MultiTypeAdapter.ItemViewHolder> {

    public interface IItem {
        int getLayout();

        int getVariableId();
    }

    private List<IItem> items;

    public MultiTypeAdapter() {
    }

    /**
     * 添加数据前先清数据列表
     *
     * @param data
     */
    public void addAllWithClear(List<? extends IItem> data) {
        if (items == null) {
            items = new ArrayList<>();
        }

        int currentSize = items.size();
        items.clear();
        notifyItemRangeRemoved(0, currentSize);
        items.addAll(data);
        notifyItemRangeInserted(0, data.size());
    }

    public void addAll(List<? extends IItem> data) {
        if (items == null) {
            items = new ArrayList<>();
        }
        int currentSize = items.size();
        items.addAll(data);
        notifyItemRangeInserted(currentSize, items.size());
    }

    public List<IItem> getValues() {
        if (items == null) {
            items = new ArrayList<>();
        }
        return items;
    }

    public void clear() {
        if (items != null) {
            items.clear();
        }
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ItemViewHolder.create(parent, viewType);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        holder.bindTo(getItem(position));
    }

    public int findPos(IItem item) {
        return items.indexOf(item);
    }

    public void setItem(IItem item) {
        clear();
        addItem(item);
    }

    public void setItems(List<? extends IItem> items) {
        clear();
        addAll(items);
    }

    public void addItem(int position, IItem obj) {
        if (items == null) {
            items = new ArrayList<>();
        }

        items.add(position, obj);
    }

    public void addItem(IItem object) {
        if (items == null) {
            items = new ArrayList<>();
        }

        items.add(object);
    }

    public int removeItem(IItem obj) {
        int position = findPos(obj);
        items.remove(obj);
        return position;
    }

    public int removeItem(int position) {
        items.remove(position);
        return position;
    }

    public IItem getItem(int position) {
        return items == null ? null : items.get(position);
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getLayout();
    }


    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final ViewDataBinding binding;

        public ItemViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        static ItemViewHolder create(ViewGroup parent, int viewType) {
            ViewDataBinding binding = DataBindingUtil.
                    inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false);
            return new ItemViewHolder(binding);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        void bindTo(IItem item) {
            binding.setVariable(item.getVariableId(), item);
            binding.executePendingBindings();
        }
    }
}
